package tank.fire;

import tank.bullet.Bullet;
import tank.bullet.BulletGroup;
import tank.Tank;
import tank.TankType;

public class DefaultFireStrategy implements FireStrategy {

    /**
     * 单例模式
     */
    private DefaultFireStrategy(){

    }

    private static class DefaultFireStrategyHolder{
        private final static DefaultFireStrategy INSTANCE = new DefaultFireStrategy();
    }

    public static DefaultFireStrategy getInstance(){
        return DefaultFireStrategyHolder.INSTANCE;
    }

    //默认开火策略: 单一方向开火, 敌我双方坦克均可进行
    @Override
    public void fire(Tank tank) {
        if (tank.getTankType() == TankType.MAIN) {
            switch (tank.getDir()) {
                case LEFT ->
                        new Bullet(BulletGroup.GOOD, tank.getX(), tank.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2, tank.getDir());
                case UP ->
                        new Bullet(BulletGroup.GOOD, tank.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2, tank.getY(), tank.getDir());
                case RIGHT ->
                        new Bullet(BulletGroup.GOOD, tank.getX() + Tank.WIDTH, tank.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2, tank.getDir());
                case DOWN ->
                        new Bullet(BulletGroup.GOOD, tank.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2, tank.getY() + Tank.HEIGHT, tank.getDir());
            }
        } else {
            switch (tank.getDir()) {
                case LEFT ->
                        new Bullet(BulletGroup.BAD, tank.getX(), tank.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2, tank.getDir());
                case UP ->
                        new Bullet(BulletGroup.BAD, tank.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2, tank.getY(), tank.getDir());
                case RIGHT ->
                        new Bullet(BulletGroup.BAD, tank.getX() + Tank.WIDTH, tank.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2, tank.getDir());
                case DOWN ->
                        new Bullet(BulletGroup.BAD, tank.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2, tank.getY() + Tank.HEIGHT, tank.getDir());
            }
        }
    }

}
